<template>
  <a-drawer
    title="入学考试科目添加（按方向）"
    width=450
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="editVisibile"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-row :gutter="48">
        <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children" disabled v-decorator="['ssyxm',{rules:[{required:true,message:'请选择院系'}]},{initialValue:postParams.ssyxm}]" @change='yxChange'>
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+"-"+ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" disabled v-decorator="[
                  'sszym',
                  {
                    rules: [{ required: true, message: '请选择专业!' }],
                  },
                  {initialValue:postParams.sszym}
                ]" @change='zyChange'>
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{i.zyh+"-"+ i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="研究方向">
              <a-select :allowClear="true" showSearch optionFilterProp="children" disabled v-decorator="['yjfxm',{rules:[{required:true,message:'请选择研究方向'}]},{initialValue:postParams.yjfxm}]" @change='yjfxChange'>
                <a-select-option v-for="i in selectMenu.yjfx" :key="i.id" :value="i.yjfxm">{{ i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="导师">
              <a-select :allowClear="true" showSearch optionFilterProp="children" disabled v-decorator="['dsxxmId',{initialValue:postParams.dsxxmId}]" >
                <a-select-option v-for="(i,j) in selectMenu.teachers" :key="j" :value="i.ID">{{ i.XM }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目类型">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['kmlxm',
              {
                rules: [{ required: true, message: '请选择科目类型!' }],
              },
              {initialValue:postParams.kmlxm}]" @change="lxChange">
                <a-select-option v-for="i in selectMenu.kmlx" :key="i.code" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目编码">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['kmbm',
              {
                rules: [{ required: true, message: '请选择科目编码!' }],
              },
              {initialValue:postParams.kmbm}]" >
                <a-select-option v-for="i in selectMenu.kmbm" :key="i.id" :value="i.kmbm">{{ i.kmbm }}-{{i.kmmc}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
  // import moment from 'moment'
  import {mapState} from 'vuex'

  const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']
export default {
  props: {
    editVisibile: {
      default: false
    },
    info: {
      default: () => ({})
    }
  },
  data () {
    return {
      formItemLayout,
      loading: false,
      postParams: {
      },
      selectMenu: {
        yx: [] ,// 院系
        zy: [] ,//专业
        yjfx: [], //研究方向
        teachers:[] ,//获取导师
        kmlx:[] ,//科目类型
        kmbm:[] //科目编码
      }
    }
  },
  beforeCreate() {
  	this.form = this.$form.createForm(this)
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
   watch: {
    info (val) {
      this.postParams = this.info

      this.form.resetFields()
      for(var item in this.info){
        // if(item == 'ssyxm' || item == 'sszym' || item == 'kmlxm' || item == 'kmbm' || item == 'id' || item == 'yjfxm' || item == 'dsxxmId' || item == 'zsxn' ){
          this.form.getFieldDecorator(item, {initialValue: this.info[item]})
        // }
      }
      this.getProssion(this.postParams.ssyxm)
      this.getTeachers(this.postParams.ssyxm)
      this.getCode(this.postParams.kmlxm)
      this.getDirec(this.postParams.sszym)
      // if(!this.form.getFieldValue('ssyxm')){
      //   this.form.setFieldsValue({'ssyxm':val.ssyxm})
      // }
      // if(!this.form.getFieldValue('sszym')){
      //   this.form.setFieldsValue({'sszym':val.sszym})
      // }
      // if(!this.form.getFieldValue('kmlxm')){
      //   this.form.setFieldsValue({'kmlxm':val.kmlxm})
      // }
      // if(!this.form.getFieldValue('kmbm')){
      //   this.form.setFieldsValue({'kmbm':val.kmbm})
      // }
      // if(!this.form.getFieldValue('yjfxm')){
      //   this.form.setFieldsValue({'yjfxm':val.yjfxm})
      // }
      //  if(!this.form.getFieldValue('dsxxmId')){
      //   this.form.setFieldsValue({'dsxxmId':val.dsxxmId})
      // }
      //  if(!this.form.getFieldValue('zsxn')){
      //   this.form.setFieldsValue({'zsxn':val.zsxn})
      // }
    }
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
       // 获取科目类型
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.$api.base.dictionaryData('TYPE0430').then(res => {
            this.selectMenu.kmlx = res.data
        })
      })

    },
    reset () {
      this.postParams = {}
    },
     yxChange(code){
      console.log('form:',this.form)
      this.form.setFieldsValue({'sszym':''})
      this.form.setFieldsValue({'yjfxm':''})
      this.form.setFieldsValue({'dsxxmId':''})
      this.$set(this.selectMenu, 'yjfx', [])
      this.$set(this.selectMenu, 'ss', [])
      this.$set(this.selectMenu, 'zy', [])
      if(code){
        this.getProssion(code)
      }


    },
    lxChange(code){
      console.log(code)
        this.form.setFieldsValue({
          'kmbm': ''
        })
        this.$set(this.selectMenu, 'kmbm', [])
        if(code){
        this.getCode(code)
        }
      },
    zyChange(code){
      this.form.setFieldsValue({'yjfxm':''})
      this.form.setFieldsValue({'dsxxmId':''})
      this.$set(this.selectMenu, 'ss', [])
      this.getDirec(code)
    },
    yjfxChange(code){
       // 获取导师
       console.log(code)
      this.getTeachers(code)
    },
    // 获取专业
    getProssion(code){
         // 获取专业
      this.$api.base.majorAll({
        lsdwh: code,zycc:'2'
      }).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    // 获取导师
    getTeachers(code){
       this.$api.base.getDirecTeachers({
        ssyxm:this.form.getFieldValue('ssyxm'),
        // zszym:this.form.getFieldValue('sszym'),
        // yjfx:code,
      }).then(res => {
        if(res.data && res.data.rows && res.data.rows.length){
          res.data.rows.map( (item,index) => {
            item.index = index
            return item
          })
          // console.log(res.data.rows)
          this.$set(this.selectMenu,'teachers',res.data.rows)
        }else{
          this.$set(this.selectMenu,'teachers',[])
        }
      })
    },
   // 获取科目编码
   getCode(code){
      this.$api.base.subjectCode({
       ssyxm:this.form.getFieldValue('ssyxm'),
       code:code
     }).then(res => {
       this.$set(this.selectMenu,'kmbm',res.data.rows)
     })
   },
   // 获取研究方向
   getDirec(code){
     this.$api.base.searchDirectory({
       ssyxm: this.form.getFieldValue('ssyxm'),
        zsxn: this.form.getFieldValue('zsxn'),
        xwccm: 2,
       sszym: code
     }).then(res => {
       this.$set(this.selectMenu, 'yjfx', res.data)
     })
   },
    // 关闭
    onClose () {
      this.reset()
      this.$emit('close')
    },
    // 保存
    handleSubmit () {
       this.form.validateFields( (err,values) => {
        if(err){
          this.$message.error("请选择必填项")
        }else{
          var kmmc ;
          this.selectMenu.kmbm.map((item,index) => {
            if(item.kmbm == this.form.getFieldValue('kmbm')){
              kmmc = item.kmmc
            }
          })
           this.info.kmmc = kmmc
          var params = Object.assign(this.info,this.form.getFieldsValue())
          // var params = Object.assign({'kmmc':kmmc,'id':this.postParams.id},this.form.getFieldsValue())
          this.$api.recruitstat.entryResultManageUpdate(params).then(res => {
            this.reset()
            this.$emit('success')
          }).finally(() => {
            this.loadingBtn = false
          })
        }
      })
    }
  }
}
</script>
<style type="text/css" scoped="">
  .ant-col-lg-12{height: 111px;}
</style>
